package ru.isys.activity;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;

import javax.swing.UIManager;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.dom4j.DocumentException;

import ru.isys.generator.Generator;
import ru.isys.generator.GeneratorImpl;
import ru.isys.generator.ModelGenerator;
import ru.isys.generator.Parser;
import ru.isys.generator.ParserImpl;
import ru.isys.view.BindItem;
import ru.isys.view.BindPanel;
import ru.isys.view.FileURIAndNsPanel;
import ru.isys.view.FormItem;
import ru.isys.view.FormPanel;
import ru.isys.view.MainView;
import ru.isys.view.ModelItem;
import ru.isys.view.ModelPanel;
import ru.isys.view.OutputXmlPanel;
import ru.isys.xml.XmlItem;
import ru.isys.xml.util.builder.ModelBuilder;
import ru.isys.xml.util.builder.OutXmlBuilder;

public class MainActivityImpl implements MainActivity
{

    LoadWSDLActivity load = new LoadWSDLActivityImpl();
    MainView view;
    Parser parser = new ParserImpl();
    Generator generator = new GeneratorImpl();
    List<XmlItem> appData;
    ModelPanel modelPanel;
    BindPanel buildPanel;
    FormPanel formPanel;

    @Override
    public void startWorking()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            FileURIAndNsPanel fileToOpenPanel = new FileURIAndNsPanel(loadWSDL());
            if (fileToOpenPanel.getFileURI() != null)
            {
                view = new MainView(fileToOpenPanel);
                // view.pack();
                view.setVisible(true);
                fileToOpenPanel.setListener(this);
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.exit(0);
        }

    }

    private String loadWSDL()
    {
        File selectedFile = load.loadWSDL();
        return selectedFile.getAbsolutePath();
    }

    @Override
    public void addMessage(String message)
    {
        view.addInfo(message);
    }

    public void onOpenFile()
    {
        List<ModelItem> elements = new ArrayList<ModelItem>();
        for (XmlItem item : appData)
        {
            elements.add(new ModelItem(item.getName(), 0));
        }

        modelPanel = new ModelPanel(this);
        modelPanel.setItems(elements);

        view.addModel(modelPanel);
        view.validate();
    }

    @Override
    public void onSaveModel()
    {

        List<BindItem> elements = new ArrayList<BindItem>();

        try
        {
            for (ModelItem modelItem : modelPanel.getCheckedModelItems())
            {
                XmlItem item = ModelBuilder.getInstance().getXmlItemByName(modelItem.getItem());
                item.setModelField(true);
                elements.add(new BindItem(item.getName(), item.getFullName(), 0));
            }
            buildPanel = new BindPanel(this);
            buildPanel.setItems(elements);
            view.addModel(buildPanel);
            view.validate();
        }
        catch (TransformerConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ParserConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onSaveBind()
    {
        List<BindItem> items = buildPanel.getBindItems();
        List<FormItem> elements = new ArrayList<FormItem>();
        formPanel = new FormPanel(this);
        try
        {
            for (BindItem item : items)
            {
                XmlItem xmlItem = ModelBuilder.getInstance().getXmlItemByName(item.getItem());
                xmlItem.setReadOnly(item.getReadonly());
                xmlItem.setRequired(item.getRequired());
                xmlItem.setType(item.getTypeField());
                elements.add(new FormItem(xmlItem.getName(), xmlItem.getFullName()));
            }
            formPanel.setItems(elements);
            view.addModel(formPanel);
            view.validate();
        }
        catch (TransformerConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ParserConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onSaveForm()
    {
        List<FormItem> elements = formPanel.getFormItems();

        try
        {
            for (FormItem item : elements)
            {
                XmlItem xmlItem = ModelBuilder.getInstance().getXmlItemByName(item.getName());
                xmlItem.setLabel(item.getLabelText());
                xmlItem.setAlert(item.getAlertText());
                xmlItem.setHint(item.getHintText());
            }
            String xml = new OutXmlBuilder(ModelBuilder.getInstance().getXmlItems()).build(ModelBuilder.getInstance().getXmlModel());
            OutputXmlPanel panel = new OutputXmlPanel(xml);
            view.addModel(panel);
            view.validate();
        }
        catch (TransformerConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ParserConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (TransformerException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (DocumentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onLoadFileAndNamespace(String fileURI, String NS) throws TransformerConfigurationException, ParserConfigurationException
    {
        view.removeFileToOpenPanel();
        parser.setListener(this);
        appData = parser.getAppData(fileURI, NS);
        onOpenFile();
    }
}
